package com.egao.common.module.company.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.constants.AttachmentConstants;
import com.egao.common.module.company.entity.Attachment;
import com.egao.common.module.company.entity.MutualInvitation;
import com.egao.common.module.company.mapper.MutualInvitationMapper;
import com.egao.common.module.company.service.AttachmentService;
import com.egao.common.module.company.service.MutualInvitationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lym
 * @date 2024/1/2 11:43
 */
@Service
public class MutualInvitationServiceImpl extends ServiceImpl<MutualInvitationMapper, MutualInvitation> implements MutualInvitationService {

    @Autowired
    private AttachmentService attachmentService;

    @Override
    public List<MutualInvitation> listPage(PageParam<MutualInvitation> pageParam) {
        return baseMapper.listPage(pageParam);
    }

    @Override
    public JsonResult detail(Integer id) {
        if (id == null) {
            throw new ParameterException("编号为空");
        }

        MutualInvitation mutualInvitation = getById(id);
        if (mutualInvitation == null) {
            throw new BusinessException("未查询到数据");
        }

        // 添加点击量
        int hits = mutualInvitation.getHits() != null ? mutualInvitation.getHits() + 1 : 1;
        mutualInvitation.setHits(hits);
        updateById(mutualInvitation);

        // 查询发布的附件
        List<Attachment> attachments = attachmentService.getAttachByBusinessId(id, AttachmentConstants.TYPE_MUTUAL_INVITATION);
        if (attachments != null && attachments.size() > 0) {
            mutualInvitation.setAttachments(attachments);
        }

        return JsonResult.ok("查询成功").setData(mutualInvitation);
    }

    @Override
    public JsonResult saveMutualInvitation(MutualInvitation mutualInvitation) {
        // 判断报名截止时间不能大于举办时间
        if (mutualInvitation.getStartTime().before(mutualInvitation.getEndTime())) {
            return JsonResult.error("报名截止时间不能大于举办时间");
        }

        if (save(mutualInvitation)) {
            attachmentService.saveAttach(mutualInvitation.getAttachments(), mutualInvitation.getId(), AttachmentConstants.TYPE_MUTUAL_INVITATION);
            return JsonResult.ok("提交成功");
        }
        return JsonResult.error("提交失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult updateMutualInvitation(MutualInvitation mutualInvitation) {
        // 判断报名截止时间不能大于举办时间
        if (mutualInvitation.getStartTime().before(mutualInvitation.getEndTime())) {
            return JsonResult.error("报名截止时间不能大于举办时间");
        }

        if (updateById(mutualInvitation)) {
            attachmentService.deleteAttach(mutualInvitation.getId(), AttachmentConstants.TYPE_MUTUAL_INVITATION);
            attachmentService.saveAttach(mutualInvitation.getAttachments(), mutualInvitation.getId(), AttachmentConstants.TYPE_MUTUAL_INVITATION);
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult deleteMutualInvitation(MutualInvitation mutualInvitation) {
        if (removeById(mutualInvitation)) {
            attachmentService.deleteAttach(mutualInvitation.getId(), AttachmentConstants.TYPE_MUTUAL_INVITATION);
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }
}
